package com.sugon.controller.device;

import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.DevPlanRuleEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.device.model.entity.DevMaintenancePlanEntity;
import com.sugon.modules.device.model.vo.CorrelateMaintenancePlanAndRuleParam;
import com.sugon.modules.device.model.vo.DevMaintenancePlanItemDateAdjustParamVO;
import com.sugon.modules.device.service.IDeviceMaintenanceManager;
import com.sugon.service.DevMaintenancePlanItemService;
import com.sugon.service.DevMaintenancePlanService;
import com.sugon.service.DevPlanRuleService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备维护计划明细操作Controller
 *
 * @author YuXD
 * @date 2020-09-05 10:17
 */
@Controller
@RequestMapping("devplanrule")
public class DevMaintenancePlanItemController {

    @Autowired
    private DevPlanRuleService devPlanRuleService;
    @Autowired
    private IDeviceMaintenanceManager deviceMaintenanceManager;
    @Autowired
    private DevMaintenancePlanService devMaintenancePlanService;
    @Autowired
    private DevMaintenancePlanItemService devMaintenancePlanItemService;
    @Autowired
    private SysMsgService msgService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<DevPlanRuleEntity> devPlanRuleList = devPlanRuleService.queryList(query);
        int total = devPlanRuleService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(devPlanRuleList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevPlanRuleEntity devPlanRule = devPlanRuleService.queryObject(id);

        return R.ok().put("devPlanRule", devPlanRule);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("devplanrule:save")
    @ResponseBody
    public R save(@RequestBody DevPlanRuleEntity devPlanRule) {
        devPlanRuleService.save(devPlanRule);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("devplanrule:update")
    @ResponseBody
    public R update(@RequestBody DevPlanRuleEntity devPlanRule) {
        devPlanRuleService.update(devPlanRule);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("devplanrule:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devPlanRuleService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 添加新的设备维护规程到指定计划当中
     *
     * @param correlateParam
     * @param bindingResult
     * @return
     */
    @RequiresPermissions("devplanmain:save")
    @NonBusinessParamValid
    @RequestMapping("/addData")
    @ResponseBody
    public R addData(@Validated @RequestBody CorrelateMaintenancePlanAndRuleParam correlateParam, BindingResult bindingResult) {
        String planId = correlateParam.getPlanId();
        DevMaintenancePlanEntity devPlanMain = devMaintenancePlanService.queryObject(planId);
        if (devPlanMain == null) {
            return R.error("维护计划不存在,请确认");
        }
        // 只可以编辑处于编制中的设备维护计划
        if (!ProcessProgressConst.NEW.equals(devPlanMain.getStatus()) && !ProcessProgressConst.NOT_PASSED.equals(devPlanMain.getStatus())) {
            return R.error("只有处于编制中的计划才可以添加规程");
        }
        String[] ruleIds = correlateParam.getRules().split(",");
        return deviceMaintenanceManager.correlateMaintenancePlanAndRule(planId, ruleIds) ? R.ok() : R.error();
    }

    /**
     * 调整设备维护项执行日期
     *
     * @param rules
     * @param bindingResult
     * @return
     */
    @RequiresPermissions("devplanmain:save")
    @RequestMapping("/updateDay")
    @ResponseBody
    @NonBusinessParamValid
    public R updateDay(@Validated @RequestBody List<DevMaintenancePlanItemDateAdjustParamVO> rules, BindingResult bindingResult) {
        return deviceMaintenanceManager.adjustDevMaintenancePlanItem(rules) ? R.ok() : R.error();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevPlanRuleEntity> list = devPlanRuleService.queryList(params);

        return R.ok().put("list", list);
    }
}
